package com.chinawayltd.ews.sample.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.model.TBranch;
import com.chinawayltd.ews.sample.model.TBranchConsumeTime;
import com.chinawayltd.ews.sample.model.TLimiter;
import com.chinawayltd.ews.sample.model.TOrder;
import com.chinawayltd.ews.sample.model.basicInfo.VehicleCustomVO;
import com.chinawayltd.ews.sample.utils.TaskDispathDateUtils;

import java.text.ParseException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * @program: greeProject
 * @description:
 * @author: xiazj
 * @create: 2018-05-07 19:57
 **/
public class TaskDispatchModel {

    private TaskDispatchModel() {
    }

    private TaskDispatchModel(Builder builder) throws ParseException {
        this.token = builder.token;
        this.data = builder.getData();
        this.orderIds = builder.orderIds;
    }

    private String token;
    private String data;
    private Set<Long> orderIds;

    public String getToken() {
        return token;
    }

    public String getData() {
        return data;
    }

    public Set<Long> getOrderIds() {
        return orderIds;
    }

    public static class Builder {

        public Builder() {
            token = UUID.randomUUID().toString();
        }

        public Builder(String token) {
            this.token = token;
        }

        private String token;
        private List<TOrder> orders;
        private Set<Long> orderIds = new HashSet<>();
        private List<VehicleCustomVO> vehicles;
        private List<TLimiter> limiters;
        private List<TBranchConsumeTime> branchConsumeTimes;
        private List<TBranch> branches;

        public Builder orders(List<TOrder> orders){
            this.orders = orders;
            return this;
        }

        public Builder vehicles(List<VehicleCustomVO> vehicles){
            this.vehicles = vehicles;
            return this;
        }

        public Builder limiters(List<TLimiter> limiters){
            this.limiters = limiters;
            return this;
        }

        public Builder branchConsumeTimes(List<TBranchConsumeTime> branchConsumeTimes){
            this.branchConsumeTimes = branchConsumeTimes;
            return this;
        }

        public Builder branches(List<TBranch> branches){
            this.branches = branches;
            return this;
        }

        private JSONArray getOrders() throws ParseException {
            JSONArray result = new JSONArray();
            if(this.orders != null && !this.orders.isEmpty()){
                for (TOrder order : this.orders) {
                    JSONObject temp = new JSONObject();
                    temp.fluentPut("date", TaskDispathDateUtils.parseCustomDate(order.getOrderDate()))
                        .fluentPut("taskid", order.getId())
                        .fluentPut("pickup", order.getStartPlaceId())
                        .fluentPut("delivery", order.getEndPlaceId())
                        .fluentPut("starttime", TaskDispathDateUtils.parseCustomDate(order.getEarlyDeliveryTime()))
                        .fluentPut("endtime", TaskDispathDateUtils.parseCustomDate(order.getLastDeliveryTime()))
                        .put("pallet", order.getTransportUnit());
                    result.add(temp);
                    orderIds.add(order.getId());
                }
            }
            return result;
        }

        private JSONObject getVehicles() throws ParseException {
            JSONObject result = new JSONObject();
            if(this.vehicles != null && !this.vehicles.isEmpty()){
                for (VehicleCustomVO vehicle : this.vehicles) {
                    JSONObject temp = new JSONObject();
                    temp.fluentPut("volume", vehicle.getCarVolume())
                        .fluentPut("workplace", vehicle.getWorkplaceBranch())
                        .fluentPut("starttime", TaskDispathDateUtils.parseCustomTime(vehicle.getBeginWorkingTime()))
                        .fluentPut("endtime", TaskDispathDateUtils.parseCustomTime(vehicle.getEndWorkingTime()))
                        .fluentPut("dayt1", TaskDispathDateUtils.parseCustomTime(vehicle.getDayShiftEarliestTime()))
                        .fluentPut("dayt2", TaskDispathDateUtils.parseCustomTime(vehicle.getDayShiftLatestTime()))
                        .fluentPut("night1", TaskDispathDateUtils.parseCustomTime(vehicle.getNightShiftEarliestTime()))
                        .fluentPut("night2", TaskDispathDateUtils.parseCustomTime(vehicle.getNightShiftLatestTime()))
                        .fluentPut("carvldtime", TaskDispathDateUtils.parseCustomDate(vehicle.getCarvldTime()))
                        .put("carvldplace", vehicle.getCarvldBranch());
                    result.put(vehicle.getCarNum(), temp);
                }
            }

            return result;
        }

        private JSONArray getLimiters()  {
            JSONArray result = new JSONArray();
            if(this.limiters != null && !this.limiters.isEmpty()){
                for (TLimiter limiter : this.limiters) {
                    JSONObject temp = new JSONObject();
                    temp.fluentPut("starttime", TaskDispathDateUtils.parseCustomTime(limiter.getBigLimiterTime()))
                        .fluentPut("endtime", TaskDispathDateUtils.parseCustomTime(limiter.getEndLimiterTime()))
                        .put("traveltime", limiter.getPeriodTime());
                    result.add(temp);
                }
            }
            return result;
        }

        private JSONArray getBranchConsumeTimes(){
            JSONArray result = new JSONArray();
            if(this.branchConsumeTimes != null && !this.branchConsumeTimes.isEmpty()){
                for (TBranchConsumeTime branchConsumeTime : this.branchConsumeTimes) {
                    JSONObject temp = new JSONObject();
                    temp.fluentPut("cartype", branchConsumeTime.getVehicleModels())
                        .fluentPut("loc1", branchConsumeTime.getStartBranch())
                        .fluentPut("loc2", branchConsumeTime.getArniveBranch())
                        .put("timecost", branchConsumeTime.getTravelTime());
                    result.add(temp);
                }
            }
            return result;
        }

        private JSONObject getBranches(){
            JSONObject result = new JSONObject();
            if(this.branches != null && !this.branches.isEmpty()){
                for (TBranch branch : this.branches) {
                    JSONObject temp = new JSONObject();
                    temp.fluentPut("name", branch.getBranchName())
                        .fluentPut("address", branch.getBranchAddr())
                        .fluentPut("longitude", branch.getLongitude())
                        .fluentPut("latitude", branch.getLatitude())
                        .fluentPut("fixedtime", branch.getConsumeTime())
                        .fluentPut("pallettime", branch.getBoardConsumeTime())
                        .fluentPut("tw1open", TaskDispathDateUtils.parseCustomTime(branch.getBigTimeOne()))
                        .fluentPut("tw1close", TaskDispathDateUtils.parseCustomTime(branch.getEndTimeOne()))
                        .fluentPut("tw2open", TaskDispathDateUtils.parseCustomTime(branch.getBigTimeTwo()))
                        .fluentPut("tw2close", TaskDispathDateUtils.parseCustomTime(branch.getEndTimeTwo()))
                        .put("ishq", branch.getBranchFromType());

                    result.put(branch.getId().toString(), temp);
                }
            }
            return result;
        }

        private JSONArray getCarLimit(){
            JSONArray result = new JSONArray();
            if(this.branches != null && !this.branches.isEmpty()){
                for (TBranch branch : this.branches) {
                    String limitCarType = branch.getUnavailableCarType();
                    if(StringUtils.isNoneBlank(limitCarType)){
                        String[] carTypeSplit = limitCarType.split(",");
                        for (String carType : carTypeSplit) {
                            JSONObject temp = new JSONObject();
                            temp.fluentPut("locid", branch.getId()).put("carlimit", Integer.parseInt(carType));
                            result.add(temp);
                        }
                    }
                }
            }
            return result;
        }

        private String getData() throws ParseException {
            JSONObject result = new JSONObject();
            result.put("tasklist", this.getOrders());
            result.put("vehicle", this.getVehicles());
            result.put("roadlimit", this.getLimiters());
            result.put("timecost", this.getBranchConsumeTimes());
            result.put("location", this.getBranches());
            result.put("carlimit", this.getCarLimit());
            return result.toJSONString();
        }

        public TaskDispatchModel build() throws ParseException { // 构建，返回一个新对象
            return new TaskDispatchModel(this);
        }
    }

}
